<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    /* 阻止选中 支持IE10及以上，IE9及以下 onselectstart="return false" */
    .unselect {
      -webkit-user-select: none;
      -moz-user-select: none;
      -khtml-user-select: none;
      -ms-user-select: none;

      /* 以下两个属性目前并未支持，写在这里为了减少风险 */
      -o-user-select: none;
      user-select: none;
    }

    .box,
    .box1 {
      width: 500px;
      height: 300px;
      border: 1px solid #000;
      margin-bottom: 20px;
      display: inline-block;
    }

    .el1 {
      width: 100px;
      height: 100px;
      background-color: red;
      display: inline-block;
    }

    .el2 {
      width: 100px;
      height: 100px;
      background-color: teal;
      display: inline-block;
    }
  </style>
</head>

<body class="unselect" onselectstart="return false">
  <div class="box"></div>
  <div class="box1"></div>
  <div></div>
  <div class="el el1" id="el1" draggable="true"></div>
  <div class="el el2" id="el2" draggable="true"></div>
</body>
<script>
  function DragDrop(box, box1, els) {
    this.box = box;
    this.box1 = box1;
    this.dragList = Array.from(els);

    this.initEl();
  }

  DragDrop.prototype.initEl = function () {
    // 给每个元素添加拖拽事件
    this.dragList.map(item => {
      item.ondragstart = function (e) {
        e.dataTransfer.setData('Text', e.target.id);
      }
    });

    comBox.call(this, this.box);
    comBox.call(this, this.box1);
  }

  function comBox(box) {
    // 给box添加拖事件 阻止默认事件
    box.ondragover = function (e) {
      e.preventDefault();
    }

    // 给box添加放事件 阻止默认事件
    box.ondrop = function (e) {
      e.preventDefault();

      // 将元素添加到box里
      this.dragList.map(item => {
        let ele = document.getElementById(e.dataTransfer.getData('Text'))
        e.target.appendChild(ele);
      })
    }.bind(this);
  }

  var els = document.querySelectorAll('.el');
  var box = document.querySelector('.box');
  var box1 = document.querySelector('.box1');
  var dd = new DragDrop(box, box1, els);
  console.log(dd);

</script>

</html>